package com.app.micaihu.view.army.activity

import android.os.Bundle
import androidx.lifecycle.Observer
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.adapter.ArmyOfficialPositionSetListAdapter
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.army.header.ArmySetOfficialPositionHeader
import com.app.micaihu.view.army.presenter.ArmyMemberPresenter
import com.app.micaihu.view.bean.ArmyCurrentMember
import com.app.micaihu.view.bean.ArmyMember
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.ViewUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.view_common_title_rv.*

/**
 * 军团官职当前列表
 */
class ArmyOfficialPositionCurrentListActivity : BaseMvpActivity<ArmyMemberContract.View, ArmyMemberPresenter>(), ArmyMemberContract.View {

    override fun getLayoutId() = R.layout.view_common_title_rv

    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
        level = bundle.getString(Config.Extra.level, "")
    }

    override fun getPresenter() = ArmyMemberPresenter(gid)

    private val armySetOfficialPositionHeader: ArmySetOfficialPositionHeader by lazy {
        ArmySetOfficialPositionHeader(thisContext, gid)
    }
    private var gid = ""
    private var level = ""

    private val armyOfficialPositionSetListAdapter: ArmyOfficialPositionSetListAdapter by lazy {
        val armyOfficialPositionSetListAdapter = ArmyOfficialPositionSetListAdapter(false)
        armyOfficialPositionSetListAdapter.headerWithEmptyEnable = true
        rvList.layoutManager = WrapContentLinearLayoutManager(thisContext)
        armyOfficialPositionSetListAdapter
    }

    override fun setData() {
        rvList.adapter = armyOfficialPositionSetListAdapter
        mPresenter?.armyAppointIndex(gid, level)
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.armyAppointIndex(gid, level)
        }

        LiveEventBus.get(Config.EventBus.armyAppointSuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "armyAppointSuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.armyAppointIndex(gid, level) }, 100)
            }
        })
    }

    override fun handleArmyAppointIndex(result: BaseBean<ArmyCurrentMember?>?) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        if (result == null) {
            armyOfficialPositionSetListAdapter.setEmptyView(errorView)
        } else {
            if (result.isSuccess()) {
                if (!armyOfficialPositionSetListAdapter.hasHeaderLayout()) {
                    armyOfficialPositionSetListAdapter.addHeaderView(armySetOfficialPositionHeader)
                }
                armySetOfficialPositionHeader.setHeaderData(result.data)
                armySetOfficialPositionHeader.setLevel(level)
                armyOfficialPositionSetListAdapter.setEmptyView(getEmptyView(R.drawable.error_icon, "暂无官职成员", 300f))
                result.data?.let {
                    armyOfficialPositionSetListAdapter.setList(it.memberList)
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }
    }

}